<?php
/**
 * Класс для обработки формы регистрации
 *
 * @author kingop
 */
class FormProcessor_Products extends FormProcessor {
    
    protected $db = null;               // объект для соединения с базой данных
    public $user = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    public $oldcat = null;
    public $subcat = null;
    public $subsubcat = null;
    public function __construct($db, $id = 0) {
        parent::__construct();
        $this->db = $db;
        $this->products = new DatabaseObject_Products($this->db);
         if ($id!='0'){
        
        $this->products->load($id);
      
        $this->oldcat = $this->products->catId;
        $this->id = $this->products->getId();
        $this->name = $this->products->name;
        $cat = $this->products->catId;
        $cats = new DatabaseObject_Categories($this->db);
        $cats->load($cat);
       // $cats = DatabaseObject_Categories::GetCatsParent($this->db, $cat);
      //  print_r($cats);
        if ($cats->parent_id==0)
        $this->catId = $cat;
        else
        {
            $cats1 = new DatabaseObject_Categories($this->db);
            $cats1->load($cats->parent_id);
             if ($cats1->parent_id==0){
                 $this->catId = $cats1->getId();
                 $this->catId1 = $cats->getId();
                 $this->subcat = DatabaseObject_Categories::GetChildOne($this->db, $cats1->getId());
             }
             else
             {
                $cats2 = new DatabaseObject_Categories($this->db);
                $cats2->load($cats1->parent_id);
             if ($cats2->parent_id==0){
                 $this->catId = $cats2->getId();
                 $this->catId1 = $cats1->getId();
                 $this->catId2 = $cats->getId();
                 $this->subcat = DatabaseObject_Categories::GetChildOne($this->db, $cats2->getId());
                 $this->subsubcat = DatabaseObject_Categories::GetChildOne($this->db, $cats1->getId());
             } 
             }
             //   print_r( $this->subcat);
        }
        $this->descr = $this->products->descr;
        $this->price = $this->products->price;
        $this->material = $this->products->material;
        $this->size = $this->products->size;
        $this->color = $this->products->color;
        $this->type = $this->products->type;
        $this->userId = $this->products->userId;
        $this->logo = $this->products->logo;
        $this->date_add = $this->products->date_add;
        $this->files = $this->products->files;
      
        }
     
    }

    public function process(Zend_Controller_Request_Abstract $request) {
        // проверим название
        $this->name = trim($request->getPost('name'));

        if (strlen($this->name) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->products->name = $this->name;
        
         // проверим alias (url)
        if ($request->getPost('type')!='')
            $this->type = trim($request->getPost('type')); 
        else 
             $this->products->type = $this->type;
        
        // проверим meta тег title 
        if ($request->getPost('descr'))
            $this->descr = trim($request->getPost('descr')); 
        else 
         $this->products->descr = $this->descr;
         
         // проверим meta тег description
        $this->price = trim($request->getPost('price'));
        $this->products->price = $this->price;
        
         // проверим meta тег keywords
        $this->material = trim($request->getPost('material'));
        $this->products->material = $this->material; 
        
        
        $this->catId = trim($request->getPost('catId'));
        $this->catId1 = trim($request->getPost('catId1'));
        $this->catId2 = trim($request->getPost('catId2'));
        if($request->getPost('catId2'))
        $this->products->catId = $this->catId2; 
        elseif($request->getPost('catId1'))
         $this->products->catId = $this->catId1;
         elseif($request->getPost('catId'))
          $this->products->catId = $this->catId;
      
        if ($this->oldcat) {
            DatabaseObject_Categories::SetProductsCount($this->db, $this->oldcat, 0);
            DatabaseObject_Categories::SetProductsCount($this->db, $this->catId, 1);
        }
        else {
                DatabaseObject_Categories::SetProductsCount($this->db, $this->catId, 1);
        }
         $this->descr = trim($request->getPost('descr'));
        $this->products->descr = $this->descr;  
        
         $this->type = trim($request->getPost('type'));
        $this->products->type = $this->type;  
        // проверим контент
        $this->size = trim($request->getPost('size'));
        $this->products->size = $this->size;
        
         // проверим родительская категория
        $this->color = trim($request->getPost('color'));
        $this->products->color = $this->color;
                
        $this->products->userId = Zend_Auth::getInstance()->getIdentity()->id;
        
          $this->file = $request->getPost('filesrc');
          $this->logo = trim($request->getPost('logo'));
          if ($this->logo)
              if($this->logo != $this->products->logo)
              {} 
              else
                if (file_exists($this->products->logo)) {
                    if (unlink($this->products->logo)) {
                }
                }
                 $this->products->logo = $this->logo;
        $this->products->date_add = date('Y-m-d H:i:s');
        $this->products->nameshop =  Zend_Auth::getInstance()->getIdentity()->nameshop; 
        $this->currency = trim($request->getPost('currency'));
        if ( $this->currency)
               $this->products->currency = $this->currency;
           else $this->products->currency = Zend_Auth::getInstance()->getIdentity()->currency;
        // если не было ошибок, сохраним 
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->products->save();
            $id = $this->products->getId();
         
            if ($this->file){
                DatabaseObject_ProductsImg::DeleteProductsImg($this->db, $id,false);
                foreach ($this->file as $val) {
                if (DatabaseObject_ProductsImg::UploadFile($this->db, $id, $val))
                
                    echo 'Yes'; 
                
                echo 'No';
              }}
        }
        
        return !$this->hasError();
    }
}

?>
